<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">{{ $t("login.title") }}</div>
            <el-form :model="loginForm" :rules="loginRules" ref="loginForm" label-width="0px" class="ms-content">
                <el-form-item prop="name">
                    <el-input v-model="loginForm.name" :placeholder="$t('login.username')" :disabled="loading">
                        <el-button slot="prepend" icon="el-icon-user-solid"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="pwd">
                    <el-input type="password" :placeholder="$t('login.password')" v-model="loginForm.pwd" show-password
                        :disabled="loading" @keyup.enter.native="handleLogin">
                        <el-button slot="prepend" icon="el-icon-question"></el-button>
                    </el-input>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click.native.prevent="handleLogin" :loading="loading" :disabled="loading"
                        class="cyber-button">
                        <span v-if="!loading">{{ $t("login.logIn") }}</span>
                        <span v-else class="loading-text">
                            <span class="loading-dots">
                                <span></span><span></span><span></span>
                            </span>
                            AUTHENTICATING
                        </span>
                    </el-button>
                </div>
            </el-form>

            <!-- 科技感加载遮罩 -->
            <div v-if="loading" class="cyber-loading-overlay">
                <div class="cyber-container">
                    <!-- 主要加载器 -->
                    <div class="cyber-loader">
                        <div class="cyber-ring cyber-ring-1"></div>
                        <div class="cyber-ring cyber-ring-2"></div>
                        <div class="cyber-ring cyber-ring-3"></div>
                        <div class="cyber-core">
                            <div class="cyber-pulse"></div>
                        </div>
                    </div>

                    <!-- 加载文字效果 -->
                    <div class="cyber-text">
                        <div class="glitch-text" data-text="ACCESSING SYSTEM">ACCESSING SYSTEM</div>
                        <div class="cyber-progress">
                            <div class="progress-bar"></div>
                            <div class="progress-dots">
                                <span></span><span></span><span></span><span></span><span></span>
                            </div>
                        </div>
                    </div>

                    <!-- 背景粒子效果 -->
                    <div class="particles">
                        <div class="particle" v-for="i in 15" :key="i" :style="getParticleStyle(i)"></div>
                    </div>

                    <!-- 扫描线效果 -->
                    <div class="scan-lines">
                        <div class="scan-line scan-line-1"></div>
                        <div class="scan-line scan-line-2"></div>
                        <div class="scan-line scan-line-3"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { login } from "@/api/admin";
import md5 from "js-md5";

export default {
    name: "Login",
    data() {
        return {
            loginForm: {
                name: localStorage.getItem("name") || "",
                pwd: '',
            },
            loginRules: {
                name: [
                    {
                        required: true,
                        trigger: "blur",
                        message: this.$t("login.rulesName"),
                    },
                ],
                pwd: [{ required: true, trigger: "blur", message: this.$t("login.rulesPwd") }],
            },
            loading: false,
            pwdType: "pwd",
            redirect: undefined,
        };
    },
    watch: {
        $route: {
            handler: function (route) {
                this.redirect = route.query && route.query.redirect;
            },
            immediate: true,
        },
    },
    methods: {
        showPwd() {
            if (this.pwdType === "pwd") {
                this.pwdType = "";
            } else {
                this.pwdType = "pwd";
            }
            this.$nextTick(() => {
                this.$refs.pwd.focus();
            });
        },
        getParticleStyle(index) {
            const delay = Math.random() * 3;
            const duration = 2 + Math.random() * 3;
            const size = 2 + Math.random() * 4;
            return {
                left: Math.random() * 100 + '%',
                top: Math.random() * 100 + '%',
                animationDelay: delay + 's',
                animationDuration: duration + 's',
                width: size + 'px',
                height: size + 'px',
            };
        },
        //点击登陆
        async handleLogin() {
            this.$refs.loginForm.validate(async (valid) => {
                if (!valid) return;

                this.loading = true;

                try {
                    localStorage.setItem("name", this.loginForm.name);
                    await login({ ...this.loginForm, pwd: md5(this.loginForm.pwd) });

                    this.$message({
                        message: this.$t("登录成功,等待跳转...") || "ACCESS GRANTED",
                        type: 'success'
                    });

                    this.$router.push("/");

                } catch (error) {
                    this.$message({
                        message: error.message || this.$t("login.loginFailed") || "ACCESS DENIED",
                        type: 'error'
                    });
                } finally {
                    setTimeout(() => {
                        this.loading = false;
                    }, 800);
                }
            });
        },
    },
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background: #0a0a0a;
    background: linear-gradient(135deg, #0c0c0c, #1a1a2e 50%, #16213e);;
    overflow: hidden;
}

.login-wrap::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background:
            radial-gradient(circle at 20% 80%, rgba(3, 255, 255, 0.22) 0%, transparent 60%),
            radial-gradient(circle at 75% 10%, rgba(142, 170, 255, 0.12) 0%, transparent 60%);
    pointer-events: none;
}

.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #00ffff;
    border-bottom: 1px solid rgba(0, 255, 255, 0.3);
    /* background: rgba(0, 0, 0, 0.1); */
    text-shadow: 0 0 10px rgba(0, 255, 255, 0.5);
    font-family: 'Courier New', monospace;
    letter-spacing: 2px;
}

.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 450px;
    transform: translate(-50%, -50%);
    border-radius: 10px;
    /* background: rgba(0, 0, 0, 0.8); */
    backdrop-filter: blur(20px);
    border: 1px solid rgba(0, 255, 255, 0.3);
    box-shadow:
        0 0 50px rgba(0, 255, 255, 0.2),
        inset 0 0 50px rgba(0, 0, 0, 0.5);
    overflow: hidden;
    padding: 50px 0;
    padding-top: 20px;
}

.ms-content {
    padding: 30px 30px;
}

.login-btn {
    text-align: center;
}

.cyber-button {
    width: 100% !important;
    height: 45px !important;
    background: linear-gradient(45deg, #00ffff 0%, #0080ff 100%) !important;
    border: 2px solid #00ffff !important;
    color: #000 !important;
    font-weight: bold !important;
    font-family: 'Courier New', monospace !important;
    letter-spacing: 1px !important;
    text-transform: uppercase !important;
    position: relative !important;
    overflow: hidden !important;
    transition: all 0.3s ease !important;
}

.cyber-button:hover:not(.is-loading) {
    box-shadow: 0 0 20px rgba(0, 255, 255, 0.6) !important;
    transform: translateY(-2px) !important;
}

.cyber-button::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
    transition: left 0.5s;
}

.cyber-button:hover::before {
    left: 100%;
}

.loading-text {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    color: #000 !important;
}

.loading-dots span {
    display: inline-block;
    width: 4px;
    height: 4px;
    margin: 0 2px;
    background: #000;
    border-radius: 50%;
    animation: loading-dots 1.4s infinite ease-in-out both;
}

.loading-dots span:nth-child(1) {
    animation-delay: -0.32s;
}

.loading-dots span:nth-child(2) {
    animation-delay: -0.16s;
}

/* 科技感加载遮罩 */
.cyber-loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.95);
    backdrop-filter: blur(10px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 9999;
    animation: cyber-fade-in 0.5s ease-out;
}

.cyber-container {
    position: relative;
    text-align: center;
}

.cyber-loader {
    position: relative;
    width: 150px;
    height: 150px;
    margin: 0 auto 40px;
}

.cyber-ring {
    position: absolute;
    border-radius: 50%;
    border: 2px solid transparent;
    animation: cyber-rotate 2s linear infinite;
}

.cyber-ring-1 {
    width: 150px;
    height: 150px;
    border-top: 2px solid #00ffff;
    border-right: 2px solid #00ffff;
    animation-duration: 2s;
}

.cyber-ring-2 {
    width: 120px;
    height: 120px;
    top: 15px;
    left: 15px;
    border-left: 2px solid #ff0080;
    border-bottom: 2px solid #ff0080;
    animation-duration: 1.5s;
    animation-direction: reverse;
}

.cyber-ring-3 {
    width: 90px;
    height: 90px;
    top: 30px;
    left: 30px;
    border-top: 2px solid #80ff00;
    border-right: 2px solid #80ff00;
    animation-duration: 1s;
}

.cyber-core {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 40px;
    height: 40px;
    transform: translate(-50%, -50%);
    background: radial-gradient(circle, #00ffff 0%, transparent 70%);
    border-radius: 50%;
}

.cyber-pulse {
    width: 100%;
    height: 100%;
    background: #00ffff;
    border-radius: 50%;
    animation: cyber-pulse 1.5s ease-in-out infinite alternate;
}

.cyber-text {
    margin-top: 20px;
}

.glitch-text {
    font-size: 24px;
    font-family: 'Courier New', monospace;
    color: #00ffff;
    text-shadow:
        0 0 10px #00ffff,
        0 0 20px #00ffff,
        0 0 30px #00ffff;
    position: relative;
    animation: glitch 2s infinite;
    letter-spacing: 3px;
}

.glitch-text::before,
.glitch-text::after {
    content: attr(data-text);
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
}

.glitch-text::before {
    animation: glitch-1 0.5s infinite;
    color: #ff0080;
    z-index: -1;
}

.glitch-text::after {
    animation: glitch-2 0.5s infinite;
    color: #80ff00;
    z-index: -2;
}

.cyber-progress {
    margin-top: 30px;
    position: relative;
}

.progress-bar {
    width: 300px;
    height: 3px;
    background: rgba(0, 255, 255, 0.2);
    margin: 0 auto;
    position: relative;
    overflow: hidden;
}

.progress-bar::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    background: linear-gradient(90deg, transparent, #00ffff, transparent);
    animation: progress-scan 2s linear infinite;
}

.progress-dots {
    display: flex;
    justify-content: center;
    gap: 10px;
    margin-top: 20px;
}

.progress-dots span {
    width: 8px;
    height: 8px;
    background: #00ffff;
    border-radius: 50%;
    animation: progress-dots 1.5s infinite ease-in-out;
    box-shadow: 0 0 10px #00ffff;
}

.progress-dots span:nth-child(1) {
    animation-delay: 0s;
}

.progress-dots span:nth-child(2) {
    animation-delay: 0.3s;
}

.progress-dots span:nth-child(3) {
    animation-delay: 0.6s;
}

.progress-dots span:nth-child(4) {
    animation-delay: 0.9s;
}

.progress-dots span:nth-child(5) {
    animation-delay: 1.2s;
}

.particles {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

.particle {
    position: absolute;
    background: #00ffff;
    border-radius: 50%;
    animation: particle-float 3s infinite ease-in-out;
    box-shadow: 0 0 6px #00ffff;
}

.scan-lines {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

.scan-line {
    position: absolute;
    width: 100%;
    height: 2px;
    background: linear-gradient(90deg, transparent, rgba(0, 255, 255, 0.8), transparent);
    animation: scan-move 3s linear infinite;
}

.scan-line-1 {
    animation-delay: 0s;
}

.scan-line-2 {
    animation-delay: 1s;
}

.scan-line-3 {
    animation-delay: 2s;
}

/* 动画定义 */
@keyframes cyber-rotate {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }
}

@keyframes cyber-pulse {
    0% {
        transform: scale(0.8);
        opacity: 1;
    }

    100% {
        transform: scale(1.2);
        opacity: 0.3;
    }
}

@keyframes cyber-fade-in {
    from {
        opacity: 0;
        transform: scale(0.9);
    }

    to {
        opacity: 1;
        transform: scale(1);
    }
}

@keyframes loading-dots {

    0%,
    80%,
    100% {
        transform: scale(0);
    }

    40% {
        transform: scale(1);
    }
}

@keyframes glitch {

    0%,
    100% {
        transform: translate(0);
    }

    20% {
        transform: translate(-2px, 2px);
    }

    40% {
        transform: translate(-2px, -2px);
    }

    60% {
        transform: translate(2px, 2px);
    }

    80% {
        transform: translate(2px, -2px);
    }
}

@keyframes glitch-1 {

    0%,
    100% {
        transform: translate(0);
    }

    10% {
        transform: translate(-2px, -1px);
    }

    20% {
        transform: translate(-1px, 1px);
    }

    30% {
        transform: translate(1px, -1px);
    }

    40% {
        transform: translate(-1px, -1px);
    }

    50% {
        transform: translate(-2px, 1px);
    }

    60% {
        transform: translate(-1px, 1px);
    }

    70% {
        transform: translate(1px, 1px);
    }

    80% {
        transform: translate(-2px, -1px);
    }

    90% {
        transform: translate(1px, -1px);
    }
}

@keyframes glitch-2 {

    0%,
    100% {
        transform: translate(0);
    }

    10% {
        transform: translate(2px, 1px);
    }

    20% {
        transform: translate(1px, -1px);
    }

    30% {
        transform: translate(-1px, 1px);
    }

    40% {
        transform: translate(1px, 1px);
    }

    50% {
        transform: translate(2px, -1px);
    }

    60% {
        transform: translate(1px, -1px);
    }

    70% {
        transform: translate(-1px, -1px);
    }

    80% {
        transform: translate(2px, 1px);
    }

    90% {
        transform: translate(-1px, 1px);
    }
}

@keyframes progress-scan {
    0% {
        transform: translateX(-100%);
    }

    100% {
        transform: translateX(400%);
    }
}

@keyframes progress-dots {

    0%,
    80%,
    100% {
        transform: scale(0.8);
        opacity: 0.5;
    }

    40% {
        transform: scale(1.2);
        opacity: 1;
    }
}

@keyframes particle-float {

    0%,
    100% {
        transform: translateY(0px) rotate(0deg);
        opacity: 1;
    }

    50% {
        transform: translateY(-20px) rotate(180deg);
        opacity: 0.5;
    }
}

@keyframes scan-move {
    0% {
        top: 0%;
        opacity: 1;
    }

    50% {
        opacity: 1;
    }

    100% {
        top: 100%;
        opacity: 0;
    }
}

/* Element UI 样式覆盖 */
.ms-login>>>.el-input--small .el-input__inner {
    height: 40px;
    background: rgba(0, 0, 0, 0.7);
    border: 1px solid rgba(0, 255, 255, 0.3);
    color: #00ffff;
}

.ms-login>>>.el-input--small .el-input__inner:focus {
    border-color: #00ffff;
    box-shadow: 0 0 10px rgba(0, 255, 255, 0.3);
}

.ms-login>>>.el-input-group__prepend {
    background: rgba(0, 255, 255, 0.1);
    border: 1px solid rgba(0, 255, 255, 0.3);
}

.ms-login>>>.el-button--primary.is-loading {
    background: linear-gradient(45deg, #00ffff 0%, #0080ff 100%) !important;
    border-color: #00ffff !important;
}

/* 响应式设计 */
@media (max-width: 480px) {
    .ms-login {
        width: 90%;
    }

    .cyber-loader {
        width: 120px;
        height: 120px;
    }

    .cyber-ring-1 {
        width: 120px;
        height: 120px;
    }

    .cyber-ring-2 {
        width: 90px;
        height: 90px;
        top: 15px;
        left: 15px;
    }

    .cyber-ring-3 {
        width: 60px;
        height: 60px;
        top: 30px;
        left: 30px;
    }

    .glitch-text {
        font-size: 18px;
    }

    .progress-bar {
        width: 250px;
    }
}
</style>